package com.lmx.healthcode.ui.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.lmx.healthcode.R;
import com.lmx.healthcode.base.MyApp;
import com.lmx.healthcode.common.base.BaseActivity;
import com.lmx.healthcode.model.bean.Student;
import com.lmx.healthcode.util.CommonUtil;
import com.lmx.healthcode.util.Const;
import com.lmx.healthcode.util.QrCodeUtil;
import com.lmx.healthcode.util.SharedPreUtil;
import com.lmx.healthcode.util.ToastUtil;
import com.wangpeiyuan.zxingbarcode.core.EncodeListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.QueryListener;

/**
 * @author Leung
 * @date 2020/9/9 16:44
 */
public class MainActivity extends BaseActivity {

    @BindView(R.id.img_menu)
    ImageView imgMenu;
    @BindView(R.id.img_qrCode)
    ImageView imgQrCode;
    @BindView(R.id.tv_generatedTime)
    TextView tvGeneratedTime;
    @BindView(R.id.tv_infoUpdateTime)
    TextView tvInfoUpdateTime;
    @BindView(R.id.tv_studentId)
    TextView tvStudentId;
    @BindView(R.id.tv_studentName)
    TextView tvStudentName;
    @BindView(R.id.tv_college)
    TextView tvCollege;
    @BindView(R.id.tv_studentClass)
    TextView tvStudentClass;
    @BindView(R.id.tv_counselor)
    TextView tvCounselor;
    @BindView(R.id.tv_counselorPhone)
    TextView tvCounselorPhone;
    @BindView(R.id.tv_dormitory)
    TextView tvDormitory;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_outSchoolTime)
    TextView tvOutSchoolTime;
    @BindView(R.id.tv_outSchoolDoor)
    TextView tvOutSchoolDoor;
    @BindView(R.id.btn_health)
    Button btnHealth;
    @BindView(R.id.btn_outSchool)
    Button btnOutSchool;
    @BindView(R.id.btn_change)
    Button btnChange;
    @BindView(R.id.swipeRefreshLayout)
    SwipeRefreshLayout swipeRefreshLayout;
    @BindView(R.id.tv_greenCode)
    TextView tvGreenCode;
    @BindView(R.id.tv_redCode)
    TextView tvRedCode;
    @BindView(R.id.tv_yellowCode)
    TextView tvYellowCode;


    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    public void initView() {
        ButterKnife.bind(this);
    }

    @Override
    public void initListener() {
        swipeRefreshLayout.setOnRefreshListener(() -> {
            if (!MyApp.isFirstCommit()) {
                getData();
            } else {
                ToastUtil.showToast("您还未提交数据!");
            }
            swipeRefreshLayout.setRefreshing(false);
        });
    }

    @Override
    public void initData() {
        if (!MyApp.isFirstCommit()) {
            getData();
        }
    }

    private void getData() {
        String objectId = SharedPreUtil.getString(this, SharedPreUtil.KEY_OBJECT_ID, "null");
        BmobQuery<Student> query = new BmobQuery<>();
        query.getObject(objectId, new QueryListener<Student>() {
            @Override
            public void done(Student student, BmobException e) {
                if (e == null) {
                    showData(student);
                    String content = Const.URL_HEAD + student.getStudentId() + "," + System.currentTimeMillis();
                    QrCodeUtil.createQrCode(content, getColor(R.color.colorPurple), new EncodeListener() {
                        @Override
                        public void onEncodeSuccess(Bitmap bitmap, String outPutPath) {
                            imgQrCode.setImageBitmap(bitmap);
                        }

                        @Override
                        public void onEncodeFail(Throwable t) {

                        }
                    });
                } else {
                    Log.d("MainActivity", "e:" + e);
                    ToastUtil.showToast("请求数据失败！错误信息：" + e.getMessage());
                }
            }
        });
    }

    private void showData(Student student) {
        tvGreenCode.setTextColor(getColor(R.color.colorPurple));
        tvYellowCode.setTextColor(getColor(R.color.colorBasicInfoText));
        tvGeneratedTime.setText(CommonUtil.getNowTime("yyyy-MM-dd HH:mm:ss"));
        tvInfoUpdateTime.setText(CommonUtil.getNowTime("yyyy-MM-dd"));
        tvStudentId.setText(String.valueOf(student.getStudentId()));
        tvStudentName.setText(student.getName());
        tvCollege.setText(student.getCollege());
        tvStudentClass.setText(student.getStudentClass());
        tvCounselor.setText(student.getCounselor());
        tvCounselorPhone.setText(String.valueOf(student.getCounselorPhone()));
        String dormitory = student.getDormitory();
        if (dormitory.equals(getString(R.string.wangtang))) {
            tvDormitory.setText(dormitory);
            tvDormitory.setTextColor(Color.rgb(242, 102, 102));
            tvDormitory.setTypeface(Typeface.DEFAULT_BOLD);
        } else {
            tvDormitory.setText(dormitory);
            tvDormitory.setTextColor(Color.parseColor("#1E2329"));
            tvDormitory.setTypeface(Typeface.DEFAULT);
        }
        tvType.setText(student.getType());
        tvOutSchoolTime.setText(String.format("%s %s",
                CommonUtil.getNowTime("yyyy-MM-dd"), student.getOutSchoolTime()));
        tvOutSchoolDoor.setText(student.getOutSchoolDoor());
    }

    @OnClick({R.id.img_close, R.id.btn_health, R.id.btn_outSchool, R.id.btn_change})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_health:
            case R.id.btn_change:
                ToastUtil.showToast("此功能尚未开发！敬请期待");
                break;
            case R.id.btn_outSchool:
                Intent intent = new Intent(MainActivity.this, OutSchoolActivity.class);
                startActivityForResult(intent, Const.OUT_SCHOOL_ACTIVITY_CODE);
                break;
            default:
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == Const.OUT_SCHOOL_ACTIVITY_CODE) {
            if (resultCode == RESULT_OK) {
                getData();
            }
        }
    }
}
